package threading;

//生产者消费者模型


import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;

public class ThreadDemo20 {
    public static void main(String[] args) {
        BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque<>();
        Thread t1 = new Thread(() -> {
            while (true) {
                try {
                    int value = blockingDeque.take();
                    System.out.println("消费元素 :" + value);

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t1.start();

        Thread t2 = new Thread(() -> {
            int value = 0;
            while (true) {
                try {
                    System.out.println("生产元素:" + value);
                    blockingDeque.put(value);
                    value ++;
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        t2.start();

        //上述代码中 , 让生产者 , 每隔 1s 生产一个元素
        //让消费者则直接消费 , 不受限制
    }
}
